package edu.ncu.dao;

import edu.ncu.entity.Publish;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface PublishDao {


    /**
     * int pId;
     * String pTitle;
     * String pContent;
     * String img1 ;
     * String img2 ;
     * String img3 ;
     * Date pTime;   //发
     * int likeNum;
     * int commentNum;
     * int pUId;    //发布
     * 根据userId获取对应的所有动态
     * @param uId
     * @return
     */
    @Select("select p.* from publish p where p.publisherId=#{uId}")
    @Results(id="pMap",value = {
            @Result(id=true,column = "id",property = "pId"),
            @Result(column = "title",property = "pTitle"),
            @Result(column = "content",property = "pContent"),
            @Result(column = "img1",property = "img1") ,
            @Result(column = "img2",property = "img2") ,
            @Result(column = "img3",property = "img3") ,
            @Result(column = "time",property = "pTime") ,
            @Result(column = "likeNum",property = "likeNum") ,
            @Result(column = "commentNum",property = "commentNum") ,
            @Result(column = "publisherId",property = "pUId") ,
            @Result(column = "html",property = "html")
    })
    List<Publish> getPublishes(int uId) ;

    /**
     * 根据动态标题查询动态
     * @param pTitle
     * @return
     */
    @Select("select * from publish where title=#{pTitle}")
    @ResultMap("pMap")
    Publish getPublishByTitle(String pTitle) ;


    @Select("select * from publish;")
    @ResultMap("pMap")
    List<Publish> getAllPublishes() ;

    /**
     * 根据动态标题查询动态
     * @param publishId
     * @return
     */
    @Select("select * from publish where id=#{publishId}")
    @ResultMap("pMap")
    Publish getPublishById(int publishId) ;

    /**
     * 插入动态
     * @param p
     * @return
     */
    @Insert("insert into publish(title,content,img1,img2,img3,time,likeNum,commentNum,publisherId) " +
            "values(#{pTitle}, #{pContent}, #{img1}, #{img2}, #{img3}, #{pTime}, #{likeNum}, #{commentNum},#{pUId})")
    boolean insertPublish(Publish p) ;

    /**
     * 删除动态
     * @param publishId
     * @return
     */
    @Delete("delete from publish where id=#{publishId}")
    boolean deletePublish(int publishId) ;


    @Update("update publish set commentNum=commentNum+1 where id=#{publishId}")
    boolean updatePublish(int publishId) ;

    @Update("update publish set likeNum=likeNum+1 where id=#{publishId}")
    boolean updatePublishGreat(int publishId) ;
}


/**
 * //定义一对多的关系映射，实现对account的封装
 *             //        @Many注解用于一对多或多对多查询使用
 *             //        select属性指定内容：查询用户的唯一标识
 *             //        column属性指定内容：用户根据id查询账户是所需要的参数
 *             //        fetchType属性指定内容:指定延时查询FetchType.LAZY或立即查询FetchType.EAGER
 *             @Result(property = "comments",column = "id",
 *                     many = @Many(select = "edu.ncu.dao.CommentDao.findCommentByPId",
 *                             fetchType = FetchType.LAZY))
 */


